import 'dart:core';

import 'package:star_author/config/base_configuration.dart';
import 'package:star_author/utils/date.dart';
import 'package:star_author/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

enum DownCountType { empty, day, hour, end }

class DownCountWidget extends StatefulWidget {
  late Date end;
  Widget Function(DateDiff, DownCountType)? builder;

  DownCountWidget({
    Key? key,
    required String end,
  }) {
    this.end = Date.parse(end);
  }

  DownCountWidget.builder({
    Key? key,
    required String end,
    required Widget Function(DateDiff, DownCountType) builder,
  }) {
    this.end = Date.parse(end);
    this.builder = builder;
  }

  DownCountWidgetState createState() => DownCountWidgetState();
}

class DownCountWidgetState extends State<DownCountWidget> {
  late DownCountType type = DownCountType.empty;
  late DateDiff diff;
  late String timerKey;
  String timerName = 'downcount';

  void downcount() {
    Date now = Date.now();
    diff = widget.end.difference(now);
    // LogUtil.v({
    //   '天：${diff.durDays}',
    //   '时：${diff.durHours}',
    //   '分：${diff.durMinutes}',
    //   '秒：${diff.durSeconds}',
    // });
    if (diff.inSeconds > 0) {
      if (diff.inDays >= 1) {
        type = DownCountType.day;
      } else if (diff.durHours >= 1 && diff.durHours <= 24) {
        type = DownCountType.hour;
      }
    } else {
      type = DownCountType.end;
    }
    setState(() {});
  }

  Widget renderTimeContainer(String text) {
    return Container(
      padding: GetPadding.only(top: 4.w, bottom: 4.w, left: 2.w, right: 2.w),
      constraints: BoxConstraints(minWidth: 18.w),
      decoration: BoxDecoration(
        borderRadius: GetRadius.all(3.w),
        color: ColorConf.orange,
      ),
      child: Text(
        text,
        textAlign: TextAlign.center,
        style: TextStyle(
          color: ColorConf.white,
          fontSize: 12.sp,
        ),
      ),
    );
  }

  Widget renderDivider(String text) {
    return Container(
      padding: GetPadding.only(left: 4.w, right: 4.w),
      child: Text(
        text,
        style: TextStyle(
          color: ColorConf.orange,
          fontSize: 12.sp,
        ),
      ),
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    timerKey = GetTimer.add(
      immediate: true,
      name: timerName,
      millisec: 1000,
      callback: (timer) {
        downcount();
      },
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    GetTimer.remove(name: timerName, key: timerKey);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    if (widget.builder == null) {
      if (type == DownCountType.day) {
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            renderTimeContainer(diff.durDays.toString()),
            renderDivider('天'),
            renderTimeContainer(Date.completion(diff.durHours)),
            renderDivider('时'),
            renderTimeContainer(Date.completion(diff.durMinutes)),
            renderDivider('分'),
          ],
        );
      } else if (type == DownCountType.hour) {
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            renderTimeContainer(Date.completion(diff.durHours)),
            renderDivider(':'),
            renderTimeContainer(Date.completion(diff.durMinutes)),
            renderDivider(':'),
            renderTimeContainer(Date.completion(diff.durSeconds)),
          ],
        );
      } else if (type == DownCountType.end) {
        return Text(
          '已超时',
          style: TextStyle(
            color: ColorConf.orange,
            fontSize: 14.sp,
          ),
        );
      } else {
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            renderTimeContainer(Date.completion(diff.durHours)),
            renderDivider(':'),
            renderTimeContainer(Date.completion(diff.durMinutes)),
            renderDivider(':'),
            renderTimeContainer(Date.completion(diff.durSeconds)),
          ],
        );
      }
    } else {
      return widget.builder!(diff, type);
    }
  }
}
